package com.ezjie.ielts.module_read.originpager;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.ezjie.ielts.R;
import com.ezjie.ielts.constant.Constants;
import com.ezjie.ielts.model.FullTextInfo;
import com.ezjie.ielts.module_read.adapter.ReadOriginpagerAdapter;
import com.ezjie.ielts.util.StringUtil;
import com.ezjie.ielts.util.UmengPages;
import com.ezjie.ielts.widget.ParallaxScollListView;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.umeng.analytics.MobclickAgent;

public class ReadOriginPagerFragment extends Fragment {

	@ViewInject(R.id.layout_listview)
	private ParallaxScollListView layout_listview;

	private BitmapUtils bUtils;
	private Context mContext;
	private List<String> lists = new ArrayList<String>();
	private ReadOriginpagerAdapter adapter;
	private ArrayList<FullTextInfo> origindata = new ArrayList<FullTextInfo>();

	private ImageView header_img;
	private TextView header_txt;
	private FullTextInfo info;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_originpager, null);
		ViewUtils.inject(this, view);
		mContext = getActivity();
		bUtils = new BitmapUtils(mContext);
		bUtils.configDefaultLoadingImage(R.drawable.default_img);// 默认背景图片
		bUtils.configDefaultLoadFailedImage(R.drawable.default_img);// 加载失败图片
		init();
		return view;
	}

	public void addDatas(List<String> lists, boolean flag) {
		this.lists = lists;
		layout_listview.setListViewPos(0);
		adapter.addDatas(lists, flag);
	}

	public void resetDatas(String category_id) {
		for (int i = 0; i < origindata.size(); i++) {
			FullTextInfo info = origindata.get(i);
			if (category_id.equals(info.category_id)) {
				header_txt.setText(Html.fromHtml(info.title_en));
				bUtils.display(header_img, info.android_img);
				List<String> lists = StringUtil.splitOriginStr(info.content);
				boolean flag = false;
				if (!info.con_img.equals("") && lists != null) {
					lists.add(info.con_img);
					flag = true;
				}
				addDatas(lists, flag);
				return;
			}
		}
	}

	@SuppressWarnings("unchecked")
	private void init() {
		View header = LayoutInflater.from(mContext).inflate(R.layout.parallax_headview, null);
		header_img = (ImageView) header.findViewById(R.id.iv_header_img);
		header_txt = (TextView) header.findViewById(R.id.tv_header_txt);
		layout_listview.setParallaxImageView(header_img);
		layout_listview.addHeaderView(header);
		/*
		 * StringBuilder strb = new StringBuilder(); for (int i = 0; i < 9; i++)
		 * {
		 * strb.append(getResources().getString(R.string.home_main_default_warn
		 * )); } for (int j = 0; j < 9; j++) { lists.add(strb.toString()); }
		 */
		adapter = new ReadOriginpagerAdapter(mContext, lists);
		layout_listview.setAdapter(adapter);
		layout_listview.setViewsBounds(ParallaxScollListView.ZOOM_X2);

		Bundle bundle = getArguments();

		info = (FullTextInfo) bundle.getSerializable(Constants.READ_SETDATA_SINGLE_ORIGIN);

		if (null != info) {
			// header_txt.setText(info.title_en);
			// bUtils.display(header_img, info.android_img);
			origindata.add(info);
		} else {
			origindata = (ArrayList<FullTextInfo>) bundle.getSerializable(Constants.READ_SETDATA_ORIGIN);
		}

		String category_id = bundle.getString(Constants.READ_ORIGIN_INDEX);
		if (null == category_id) {
			category_id = "";
		}
		resetDatas(category_id);
	}

	@Override
	public void onResume() {
		super.onResume();
		MobclickAgent.onPageStart(UmengPages.PAGE_ORIGIN);
		MobclickAgent.onResume(mContext);

	}

	@Override
	public void onPause() {
		super.onPause();
		MobclickAgent.onPageEnd(UmengPages.PAGE_ORIGIN);
		MobclickAgent.onPause(mContext);
	}

}
